package com.hongchen.wechat.pay;

import com.hongchen.model.Redpack;
import com.hongchen.wechat.config.Api;
import com.jfinal.kit.PropKit;
import com.jfinal.weixin.sdk.api.ApiResult;
import com.jfinal.weixin.sdk.api.RedPackApi;
import weixin.popular.util.SignatureUtil;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

/**
 * Created by 录康哲 on 2018/3/21.
 */
public class RedPack extends Api {
    //用于储存用户数据
    ApiResult apiResult;

    public void index(){
        this.redirect("https://open.weixin.qq.com/connect/oauth2/authorize?appid="+appId()+"&redirect_uri=http://"+chengDong()+"/wx/user&response_type=code&scope=snsapi_userinfo&state=#wechat_redirect");
    }

    public void user(){
        //获取用户数据
        apiResult=userdetail();
        System.out.println(apiResult.getJson());
    }

    static String str;

    static String time;

    /**
     *
     * @param openid
     * @param total_amount 金额
     * @param wishing 红包祝福语
     * @param act_name 活动名称
     * @param remark 备注
     */
    public void redpack(String openid,String total_amount,String wishing,String act_name,String remark){

        str= getRandomStringByLength(32);

        time=time();

        String sign=qm(openid,total_amount,wishing,act_name,remark);

        Map m=new HashMap();
        //随机字符串
        m.put("nonce_str",str);
        //签名
        m.put("sign",sign);
        //商户订单号
        m.put("mch_billno",time);
        //商户号
        m.put("mch_id",PropKit.use("weixin.properties").get("mch_id"));
        //公众账号appid
        m.put("wxappid",PropKit.use("weixin.properties").get("wxappid"));
        //商户名称
        m.put("send_name",PropKit.use("weixin.properties").get("send_name"));
        //用户openid
        m.put("re_openid",openid);
        //付款金额(/分)
        m.put("total_amount",total_amount);
        //红包发放总人数
        m.put("total_num","1");
        //红包祝福语
        m.put("wishing",wishing);
        //Ip地址
        m.put("client_ip","127.0.0.1");
        //活动名称
        m.put("act_name",act_name);
        //备注
        m.put("remark",remark);

        try{

            Redpack redpack=new Redpack();

            redpack.setNonceStr(str);

            redpack.setSign(sign);

            redpack.setMchBillno(time);

            redpack.setMchId(PropKit.use("weixin.properties").get("mch_id"));

            redpack.setWxappid(PropKit.use("weixin.properties").get("wxappid"));

            redpack.setSendName(PropKit.use("weixin.properties").get("send_name"));

            redpack.setReOpenid(openid);

            redpack.setTotalAmount(total_amount);

            redpack.setTotalNum("1");

            redpack.setWishing(wishing);

            redpack.setActName(act_name);

            redpack.setRemark(remark);

            if(redpack.save()){

                RedPackApi.sendRedPack(m,"D:\\Idea_Space\\xwscrm\\src\\main\\webapp\\apiclient_cert.p12",PropKit.use("weixin.properties").get("mch_id"));

            }
        }catch (Exception e){

            e.printStackTrace();

        }


    }

    //生成随机字符串
    public static String getRandomStringByLength(int length) {

        String base = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";

        Random random = new Random();

        StringBuffer sb = new StringBuffer();

        for (int i = 0; i < length; i++) {

            int number = random.nextInt(base.length());

            sb.append(base.charAt(number));

        }

        return sb.toString();

    }

    //生成签名
    public String qm(String openid,String total_amount,String wishing,String act_name,String remark){

        Map m=new HashMap();
        //随机字符串
        m.put("nonce_str",str);
        //商户订单号
        m.put("mch_billno",time);
        //商户号
        m.put("mch_id",PropKit.use("weixin.properties").get("mch_id"));
        //公众账号appid
        m.put("wxappid",PropKit.use("weixin.properties").get("wxappid"));
        //商户名称
        m.put("send_name",PropKit.use("weixin.properties").get("send_name"));
        //用户openid
        m.put("re_openid",openid);
        //付款金额(/分)
        m.put("total_amount",total_amount);
        //红包发放总人数
        m.put("total_num","1");
        //红包祝福语
        m.put("wishing",wishing);
        //Ip地址
        m.put("client_ip","127.0.0.1");
        //活动名称
        m.put("act_name",act_name);
        //备注
        m.put("remark",remark);

        return SignatureUtil.generateSign(m,"VNcEXNuyVbkzkLSOJi2vlz5tXbHchaib").toString();

    }
    public String time(){

        Long s= new Date().getTime();

        Random random=new Random();

        int result=random.nextInt(10);

        return s.toString();

    }
}
